package itsm.isperp.module.service.configuration;

import java.lang.reflect.ParameterizedType;

import javax.servlet.http.HttpServletRequest;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.service.BaseService;
import itsm.isperp.framework.web.response.ResultMessage;
import itsm.isperp.module.entity.configuration.ConfCiRelationTypeProperty;
import itsm.isperp.module.repository.configuration.ConfCiRelationTypePropertyMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * 服务
 * 
 * @author lizx
 * @date 2016-06-12 16:17
 */
@Service
public class ConfCiRelationTypePropertyService extends
		BaseService<ConfCiRelationTypeProperty> {

	@Autowired
	protected ConfCiRelationTypePropertyMapper confCiRelationTypePropertyMapper;

	@Override
	public ConfCiRelationTypePropertyMapper getRepository() {
		return this.confCiRelationTypePropertyMapper;
	}

	@Override
	public int saveOrUpdate(ConfCiRelationTypeProperty entity,
			HttpServletRequest req) {
		Assert.notNull(entity, "对象不能为空");

		this.saveOrUpdateAndSpecific(entity, req);
		updateRelationTypePropertyCount(entity.getRelationTypeId());
		return 1;
	}

	public ResultMessage delete(String id) {
		ConfCiRelationTypeProperty entity = this.findOne(id);
		super.delete(id);

		updateRelationTypePropertyCount(entity.getRelationTypeId());
		return ResultMessage.success();

	}

	private void updateRelationTypePropertyCount(String relationTypeId) {
		JdbcTemplate jdbcTemplate = ContextHolder.getSpringBean("jdbcTemplate");
		jdbcTemplate
				.update("UPDATE conf_ci_relation_type a SET relation_property_count=(SELECT COUNT(*) FROM conf_ci_relation_type_property b WHERE a.id=b.relation_type_id) WHERE a.id='"
						+ relationTypeId + "'");

	}
}
